package Thread;


import java.util.ArrayList;
import java.util.Random;

public class algo{
	public String CallPutFlag;
	public double v;
	public double b;
	public double S;
	public double X;
	public double T;
	public double r;
	public int nSteps;
	public int nSimulations ;
	
	public double Drift;
	public double Vsqrdt;
	public double Sum;
	public double St;
	public double dt;
	public int z;
	
	public algo(String CallPutFlag, double S,double X, double r, double T, double b, double v, int nSteps, int nSimulations){
		this.CallPutFlag=CallPutFlag;
		this.dt=T/nSteps;
		this.Drift= (b - java.lang.Math.pow(v, 2) / 2 ) * this.dt;
		this.Vsqrdt=v*java.lang.Math.sqrt(this.dt);
		if(CallPutFlag == "c"){
			this.z=1;
		}else{
			this.z=-1;
		}
		this.Sum=0;
		this.S=S;
		this.X=X;
		this.T=T;
		this.r=r;
		this.nSteps=nSteps;
		this.nSimulations=nSimulations;
		this.b=b;
		this.v=v;
	}
	
	public double thread_compute() throws InterruptedException{
		int i;
		ArrayList <subAlgo> round = new ArrayList<subAlgo>(nSimulations);
		ArrayList <Thread> t = new ArrayList <Thread>(nSimulations);
		
		for(i=0;i<this.nSimulations;i++){
			round.add(new subAlgo(this));
			t.add(new Thread(round.get(i)));
			t.get(i).start();
		}for(i=0;i<this.nSimulations;i++) t.get(i).join();
		return java.lang.Math.exp(-r * T)*(Sum / nSimulations);
	}
	
	public double normal_compute(){
		Random rand = new Random(); 
		int i, j;
		double sum = 0; 

		for (i=0; i<nSimulations; i++){
			St = S; 
			for (j=0; j<nSteps; j++){
				St = St * java.lang.Math.exp(Drift + Vsqrdt * rand.nextGaussian()); 
			}
			sum = sum + java.lang.Math.max(z*(St-X), 0); 
		}
		return java.lang.Math.exp(-r*T*(sum/nSimulations));
	}
}
